<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Resources\Common\DataResource;
use App\Http\Resources\Common\DatasResource;
use App\Http\Resources\Common\TreeResource;
use App\Models\Game\Config\CurrencyCustom;
use App\Models\Game\Config\Currency;
use App\Models\Game\Config\Game;
use App\Models\Game\User\User;
use App\Models\Game\Common\Dictionary;
use App\Services\Game\UserService;
use App\Services\Game\Other\DictionaryService;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;

class CommonController extends Controller
{

    /**
     * 获取游戏数据
     */
    public function getGameList(Request $request)
    {
        $query = Game::query();

        // 类型筛选
        if (!!($type = $request->input('type'))) {
            $query->where('type', $type);
        }

        // 软删除筛选【默认排除软删除数据】
        if ((boolean)$request->input('withTrashed', false)) {
            $query->withTrashed();
        }

        return new DataResource(
            $query->get(['type', 'code', 'name'])->groupBy('type')
        );
    }

    /**
     * 获取平台数据列表
     */
    public function getOrgList(Request $request)
    {
        // 获取授权平台
        $orgs = $request->user()->getPermissionOrgs();

        return new DataResource([
            'dataList' => $orgs->keyBy('code')->toArray(),
            'treeList' => TreeResource::make($orgs)->toTree($request),
        ]);
    }

    /**
     * 获取玩家数据【搜索】
     */
    public function searchUsers(Request $request)
    {
        $collection = UserService::make()->search($request);

        return new DataResource($collection);
    }

    /**
     * 获取玩家数据【列表】
     */
    public function getUserList(Request $request)
    {
        list ($collection, $total) = UserService::make()->getList($request);

        return DatasResource::make($collection)->setPaginateTotal($total);
    }

    /**
     * 获取玩家数据【多条】
     */
    public function getUsers(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required|array|min:1'
        ]);
        $ids = $request->input('ids');

        $users = User::findOrFail($ids);

        return new DataResource($users);
    }

    /**
     * 获取玩家数据【单条】
     */
    public function getUser(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->input('id');

        $user = User::findOrFail($id);

        return new DataResource($user);
    }

    /**
     * 获取字典数据【列表】
     */
    public function getDictionaryList(Request $request)
    {
        list ($collection, $total) = DictionaryService::make()->getList($request);

        return DatasResource::make($collection)->setPaginateTotal($total);
    }

    /**
     * 获取字典数据【单条】
     */
    public function getDictionary(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->input('id');

        $dictionary = Dictionary::withTrashed()->findOrFail($id);

        return new DataResource($dictionary);
    }

    /**
     * 获取币种数据【列表】
     */
    public function getCurrencyList(Request $request)
    {
        $query = Currency::query();
        // 类型筛选
        if ($request->filled('type')) {
            $query->where('type', $request->input('type'));
        }
        $currencies = $query->get('currency');

        // 判断是否获取自定义币种
        if (!$request->has('system')) {
            // 自定义币种
            $customs = CurrencyCustom::all('currency');
            // 合并币种
            $currencies = $currencies->concat($customs);
        }

        // 排除币种
        if (!App::environment('prod')) {
            $currencies = $currencies->reject(function ($item){
                return Str::startsWith($item['currency'], 'CNY');
            });
        }

        return new DataResource(
            $currencies->sortBy('currency')->values()->all()
        );
    }
}
